import 'package:boss/ui/page/takeoutManagement/page/afterSaleList/provider/after_sale_list_provider.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:boss/ui/widget/search_bar.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker_fork/flutter_cupertino_date_picker_fork.dart';
import 'package:provider/provider.dart';

import 'after_sale_item.dart';

class AfterSaleBrowser extends StatefulWidget {
  @override
  _AfterSaleBrowserState createState() => _AfterSaleBrowserState();
}

class _AfterSaleBrowserState extends State<AfterSaleBrowser> {
  onSearch() {
    var provider = Provider.of<AfterSaleListProvider>(
      context,
      listen: false,
    );
    provider.loaderPullRefreshKey.currentState.refresh();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(
            top: 30.w,
          ),
          padding: EdgeInsets.symmetric(
            horizontal: 30.w,
          ),
          child: Row(
            children: <Widget>[
              Expanded(
                child: SearchBar(
                  hintText: '请输入要查询的订单号或手机号',
                  onChanged: (value) {
                    var provider = Provider.of<AfterSaleListProvider>(
                      context,
                      listen: false,
                    );
                    provider.keyword = value;
                  },
                  onEditingComplete: onSearch,
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 15.w),
                child: RadiusButton(
                  width: 110.w,
                  height: 60.w,
                  title: '查询',
                  onTap: onSearch,
                ),
              ),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  var provider = Provider.of<AfterSaleListProvider>(
                    context,
                    listen: false,
                  );

                  DatePicker.showDatePicker(
                    context,
                    initialDateTime: provider.startTime,
                    locale: DateTimePickerLocale.zh_cn,
                    pickerMode: DateTimePickerMode.date,
                    maxDateTime: DateTime.now(),
                    onConfirm: (dateTime, _) {
                      provider.startTime = dateTime;
                      provider.loaderPullRefreshKey.currentState.refresh();
                    },
                  );
                },
                child: Padding(
                  padding: EdgeInsets.all(
                    15.w,
                  ),
                  child: BaseImage(
                    'assets/image/calendar_icon.png',
                    height: 30.w,
                    width: 30.w,
                  ),
                ),
              ),
            ],
          ),
        ),
        Expanded(
          child: Consumer<AfterSaleListProvider>(
            builder: (context, provider, child) {
              return LoaderPullRefresh(
                key: provider.loaderPullRefreshKey,
                loadTask: provider.loadData,
                refreshTask: provider.refreshData,
                builder: (context) {
                  return ListView.builder(
                    padding: EdgeInsets.symmetric(
                      horizontal: 30.w,
                    ),
                    itemBuilder: (context, index) {
                      var current = provider.orders[index];

                      return AfterSaleItem(data: current);
                    },
                    itemCount: provider.orders.length,
                  );
                },
              );
            },
          ),
        ),
      ],
    );
  }
}
